<html>
<head><meta charset="utf-8"><title>Use-after-move · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html">Use-after-move</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219815180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/219815180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#219815180">(Dec 14 2020 at 09:10)</a>:</h4>
<p>If, <a href="#narrow/stream/131828-t-compiler/topic/miri.20use-after-free/near/219813804">as Ralf just mentioned in another thread</a>, it's not currently UB to read after moving something, does that mean I could workaround <a href="https://github.com/rust-lang/rust/issues/79914">https://github.com/rust-lang/rust/issues/79914</a> by forgetting something, but then continuing to use it at its original address?</p>



<a name="219819373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/219819373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#219819373">(Dec 14 2020 at 09:54)</a>:</h4>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span> I don't think so. All I said is that the read itself is not UB, but if the destructor actually does something destructive, there can still be UB caused by use-after-move.</p>



<a name="219819403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/219819403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#219819403">(Dec 14 2020 at 09:54)</a>:</h4>
<p>but then the reason is a use-after-free (if it was a <code>Box</code>), it's not the move itself that caused the UB</p>



<a name="219819434"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/219819434" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#219819434">(Dec 14 2020 at 09:55)</a>:</h4>
<p>so in a sense use-after-move is <em>library UB</em> as you cannot tell what the type might be doing in its destructor. it's not currently language UB though.</p>



<a name="219893402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/219893402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#219893402">(Dec 14 2020 at 19:50)</a>:</h4>
<p>Sorry, I'm not following here.  If I'm forgetting it, where's the destructor coming in?  Or can the "move" into <code>mem::forget</code> cause a problem with using <code>drop_in_place</code> on the original locations?  Or when re-moving them from the same location?</p>



<a name="219917289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/219917289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#219917289">(Dec 14 2020 at 23:28)</a>:</h4>
<p>it could be <em>any</em> operation that invalidates the old fields -- like moving a <code>Vec</code> and then pushing into it, if that <code>realloc</code>s then the stale moved-from copy will have a dangling pointer.</p>



<a name="219922055"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/219922055" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#219922055">(Dec 15 2020 at 00:20)</a>:</h4>
<p>Right, but I'm forgetting after the move, so there are <em>no</em> operations done on it after the move.</p>



<a name="219922845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/219922845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#219922845">(Dec 15 2020 at 00:29)</a>:</h4>
<p>ah, maybe <span class="user-mention silent" data-user-id="120791">RalfJ</span> missed the "forgetting" detail just as I did</p>



<a name="219922979"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/219922979" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#219922979">(Dec 15 2020 at 00:30)</a>:</h4>
<p>it surprises me that Rust doesn't try to reuse moved-from storage, but maybe there's not much to be gained</p>



<a name="219960135"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/219960135" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#219960135">(Dec 15 2020 at 10:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="138448">cuviper</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Use-after-move/near/219922979">said</a>:</p>
<blockquote>
<p>it surprises me that Rust doesn't try to reuse moved-from storage, but maybe there's not much to be gained</p>
</blockquote>
<p>on the MIR level, that would basically mean adding <code>StorageEnd</code> right after the move</p>



<a name="219960157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/219960157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#219960157">(Dec 15 2020 at 10:32)</a>:</h4>
<p>if we do that then indeed "use-after-move" could be UB for accessing deallocated memory</p>



<a name="219960177"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/219960177" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#219960177">(Dec 15 2020 at 10:32)</a>:</h4>
<p>but currently, MIR lowering doesn't do that -- intentionally or not, I do not know</p>



<a name="219960205"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/219960205" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#219960205">(Dec 15 2020 at 10:32)</a>:</h4>
<p>also, what about moving from <code>x.1</code> but not from the other field? then we cannot deallocate that part of the storage...</p>



<a name="220018311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220018311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220018311">(Dec 15 2020 at 18:05)</a>:</h4>
<p>Honestly I expected that there'd be a StorageEnd after the move</p>



<a name="220018471"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220018471" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220018471">(Dec 15 2020 at 18:06)</a>:</h4>
<p>I would expect to be able to mir-optimize <code>_2 = move _1; _3 = move _2; _4 = move _3</code> to <code>_4 = move _1</code> because move should imply the intermediate variable has no other uses</p>



<a name="220092554"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220092554" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220092554">(Dec 16 2020 at 09:18)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Use-after-move/near/220018471">said</a>:</p>
<blockquote>
<p>I would expect to be able to mir-optimize <code>_2 = move _1; _3 = move _2; _4 = move _3</code> to <code>_4 = move _1</code> because move should imply the intermediate variable has no other uses</p>
</blockquote>
<p>there's two ways to achieve this:</p>
<ul>
<li>make sure there are StorageEnd</li>
<li>make <code>move</code> a destructive read that leaves the src memory uninitialized</li>
</ul>
<p>The latter is problematic because it makes "move" reads a mutating operation (which needs to be considered carefully for optimizations, and which will break some type-based sanity checks we have in miri that currently ensure that reads are read-only); the former is problematic because it is unclear how to scale this to moves out of places with projections.</p>



<a name="220146830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220146830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220146830">(Dec 16 2020 at 17:34)</a>:</h4>
<p>Is this something we <em>can</em> change though, at the language level?</p>



<a name="220146925"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220146925" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220146925">(Dec 16 2020 at 17:35)</a>:</h4>
<p>that is, if it's not already declared as UB to access storage after a move, then some code could be using this</p>



<a name="220147158"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220147158" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220147158">(Dec 16 2020 at 17:37)</a>:</h4>
<p>or maybe this is super undefined, in the sense that we haven't decided whether it's UB or not</p>



<a name="220147612"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220147612" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220147612">(Dec 16 2020 at 17:41)</a>:</h4>
<p>Feels like the "we haven't been clear about it, so we can probably make it UB, unless there turns out to be a bunch of people relying on it for a good reason"</p>



<a name="220152879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220152879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220152879">(Dec 16 2020 at 18:21)</a>:</h4>
<p>then we could start with that declaration of UB, whether or not we actually add the <code>StorageEnd</code>to let it be reused</p>



<a name="220152915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220152915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220152915">(Dec 16 2020 at 18:21)</a>:</h4>
<p>(issues for lang vs. compiler respectively)</p>



<a name="220411479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220411479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220411479">(Dec 18 2020 at 18:35)</a>:</h4>
<p><span class="user-mention silent" data-user-id="138448">cuviper</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Use-after-move/near/220152879">said</a>:</p>
<blockquote>
<p>then we could start with that declaration of UB, whether or not we actually add the <code>StorageEnd</code>to let it be reused</p>
</blockquote>
<p>well we have to start by figuring out what exactly we mean by "use-after-move is UB". As always, "use" is a rather ambiguous term.</p>



<a name="220411510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220411510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220411510">(Dec 18 2020 at 18:35)</a>:</h4>
<p>in a way that's precise enough that I could put it in Miri ;)</p>



<a name="220411536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220411536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220411536">(Dec 18 2020 at 18:35)</a>:</h4>
<p>things are fairly clear when it is <code>move(_1)</code> (deallocate the memory backing that local), but what about <code>move(_1.field.field)</code>?</p>



<a name="220412327"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220412327" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220412327">(Dec 18 2020 at 18:42)</a>:</h4>
<p>my intuition is that a "use" is reading or writing that storage. simply pointing to it is fine, since pointers don't have to have a valid target anyway</p>



<a name="220412422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220412422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220412422">(Dec 18 2020 at 18:43)</a>:</h4>
<p>and with partial moves, it would only be UB to read/write the moved part</p>



<a name="220412547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220412547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220412547">(Dec 18 2020 at 18:44)</a>:</h4>
<p>I trust you to make a formal definition from "what <span class="user-mention silent" data-user-id="138448">cuviper</span>  thinks" <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="220464869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220464869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220464869">(Dec 19 2020 at 10:56)</a>:</h4>
<p>well, the only way I know to make that formal is to add a new state for storage: now it can not just be uninitialized, but also "invalid", where reads are UB (or do they yield "uninit"?) and writes are UB.</p>



<a name="220464910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220464910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220464910">(Dec 19 2020 at 10:56)</a>:</h4>
<p>as for pointers... is a pointer to such storage considered "dereferencable"?</p>



<a name="220464924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220464924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220464924">(Dec 19 2020 at 10:57)</a>:</h4>
<p>this is more complicated than past proposals where the memory was just made uninitialized (in which case writes would still work)</p>



<a name="220464931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220464931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220464931">(Dec 19 2020 at 10:57)</a>:</h4>
<p>I do not entirely understand what the benefit is of <span class="user-mention" data-user-id="138448">@cuviper</span>'s proposal over "move deinitializes memory".</p>



<a name="220470773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220470773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220470773">(Dec 19 2020 at 13:54)</a>:</h4>
<blockquote>
<p>as for pointers... is a pointer to such storage considered "dereferencable"?</p>
</blockquote>
<p>I would assume not. dereferencable, to me, implies a certain number of bytes are "reachable" from a pointer, and those bytes do not exist. It could be considered that a move doesn't merely logically uninitialize the source, but logically deallocates the storage (thus we have the same question for a different scenario, would you expect a pointer into a dropped box to be dereferencable). </p>
<p>I think reads should be promoted to UB and not uninit, because that would allow an implementation where the location is actually destroyed IE. inaccessible, in a sparse address space. Note that, of course, this would allow an implementation that merely yields uninit, for those that have contiguous address spaces.</p>



<a name="220471279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220471279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220471279">(Dec 19 2020 at 14:07)</a>:</h4>
<blockquote>
<p>It could be considered that a move doesn't merely logically uninitialize the source, but logically deallocates the storage </p>
</blockquote>
<p>But that means allocations can have "gaps" in them, so that is still a quite fundamental change to the memory model</p>



<a name="220472743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220472743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220472743">(Dec 19 2020 at 14:47)</a>:</h4>
<p>If a move from a local causes the storage for that local to be deallocated, but a move from a field of a local doesn't cause that field to be deallocated, SROA (scalar replacement of array) would not be allowed as it could cause a move out of a field to turn into a move out of a local, thereby introducing a deallocation that didn't previously exist.</p>



<a name="220473049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220473049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220473049">(Dec 19 2020 at 14:55)</a>:</h4>
<blockquote>
<p>But that means allocations can have "gaps" in them, so that is still a quite fundamental change to the memory model</p>
</blockquote>
<p>I should add, I only mean for this to applied at the top level, moving a "complete object" as it were. Partial moving could still just render the storage uninit.</p>



<a name="220473096"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move/near/220473096" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Use-after-move.html#220473096">(Dec 19 2020 at 14:56)</a>:</h4>
<p><span class="user-mention silent" data-user-id="257758">Connor Horman</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Use-after-move/near/220473049">said</a>:</p>
<blockquote>
<blockquote>
<p>But that means allocations can have "gaps" in them, so that is still a quite fundamental change to the memory model</p>
</blockquote>
<p>I should add, I only mean for this to applied at the top level, moving a "complete object" as it were. Partial moving could still just render the storage uninit.</p>
</blockquote>
<p>As <span class="user-mention" data-user-id="133247">@bjorn3</span> pointed out, that doesn't work in the presence of SROA</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>